<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
	<head>
		<th:block th:include="include :: header('新增菜单')" />
		<th:block th:include="include :: select2-css" />
	</head>
	<body class="white-bg">
		<div class="wrapper wrapper-content animated fadeInRight ibox-content">
			<form class="form-horizontal m" id="formEdit"    >
				<input id="id" name="id" type="hidden"  />

				<div class="form-group">
					<label class="col-sm-3 control-label is-required">菜单类型：</label>
					<div class="col-sm-8">
						<label class="radio-box"> <input type="radio" name="menuLevel" id="menuLevel1" value="1" /> 目录 </label>
						<label class="radio-box"> <input type="radio" name="menuLevel" id="menuLevel2" value="2" /> 菜单 </label>
						<label class="radio-box"> <input type="radio" name="menuLevel" id="menuLevel3" value="3" /> 按钮 </label>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">上级菜单：</label>
					<div class="col-sm-8">
						<select id="parentMenuId" name="parentMenuId" class="form-control m-b">

						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label is-required">菜单名称：</label>
					<div class="col-sm-8">
						<input class="form-control" type="text" name="menuName" id="menuName" required>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">请求地址：</label>
					<div class="col-sm-8">
						<input id="menuUrl" name="menuUrl" class="form-control" type="text">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">打开方式：</label>
					<div class="col-sm-8">
						<select id="target" name="target" class="form-control m-b">
							<option value="menuItem">页签</option>
							<option value="menuBlank">新窗口</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">权限标识：</label>
					<div class="col-sm-8">
						<textarea id="permission" name="permission" class="form-control" ></textarea>
						<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 控制器中定义的权限标识，如：@MarsPermission("")</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label is-required">显示排序：</label>
					<div class="col-sm-8">
						<input class="form-control" type="text" name="priority"  id="priority" required>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">图标：</label>
					<div class="col-sm-8">
						<input id="icon" name="icon" class="form-control" type="text" placeholder="选择图标">
						<div class="ms-parent" style="width: 100%;">
							<div class="icon-drop animated flipInX" style="display: none;max-height:200px;overflow-y:auto">
								<div data-th-include="system/menu/icon"></div>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-3 control-label">菜单状态：</label>
					<div class="col-sm-3">
						<label class="radio-box"> <input type="radio" name="visible" value="0" /> 显示 </label>
						<label class="radio-box"> <input type="radio" name="visible" value="1" /> 隐藏 </label>
					</div>
					<label class="col-sm-2 control-label is-refresh" title="打开菜单选项卡是否刷新页面">是否刷新：<i class="fa fa-question-circle-o"></i></label>
					<div class="col-sm-3 is-refresh">
						<div class="radio-box">
							<input type="radio" id="refresh-no" name="isRefresh" value="1" checked>
							<label for="refresh-no">否</label>
						</div>
						<div class="radio-box">
							<input type="radio" id="refresh-yes" name="isRefresh" value="0">
							<label for="refresh-yes">是</label>
						</div>
					</div>
				</div>
			</form>
		</div>
		<th:block th:include="include :: footer" />
		<th:block th:include="include :: select2-js" />
		<script>

			var prefix = ctx + "sys/menu";
			var queryUrl = prefix +"/selectById";
			$("#formEdit").validate({
				onkeyup: false,
				rules:{
					menuLevel:{
						required:true,
					},
					menuName:{
						required:true,
					},
					priority:{
						digits:true
					},
				},
				messages: {
					menuLevel:{
						required:"请选择"
					},
					priority:{
						digits:"请输入数字",
					}
				},
				focusCleanup: true
			});

			function submitHandler() {
				if ($.validate.form()) {
					$.operate.save(prefix + "/updateById", $('#formEdit').serializeJson(),null,true);
				}
			}

			$(function() {
				$("input[name='icon']").focus(function() {
					$(".icon-drop").show();
				});
				$("#formEdit").click(function(event) {
					var obj = event.srcElement || event.target;
					if (!$(obj).is("input[name='icon']")) {
						$(".icon-drop").hide();
					}
				});
				$(".icon-drop").find(".ico-list i").on("click", function() {
					$('#icon').val($(this).attr('class'));
				});


				$('input[name=\'menuLevel\']').on('ifChecked', function(event){
					var menuType = $(event.target).val();
					selectMenuLevel(menuType,null);
				});
				var id = getQuery("id");
				$.operate.post2(queryUrl,{"id":id},function (data) {
					if (data.code==0){
						var row = data.data;
						if (row){
							console.log(row)


							$.each(row,function (k,v) {
								if (k=='menuLevel'){
									radioChecked("menuLevel",row.menuLevel,true)
									selectMenuLevel(row.menuLevel,row.parentMenuId);
								}else if (k=="visible"){
									radioChecked("visible",row.visible,true);
								}else if (k=="isRefresh"){
									radioChecked("isRefresh",row.isRefresh,true);
								}
								$("#"+k).val(v);
							})
						}
					}
					return ;
				})

			});
			function selectMenuLevel(menuType,parentMenuId) {
				if (menuType == "1") {
					$("#parentMenuId").parents(".form-group").hide();

					$("#menuUrl").parents(".form-group").hide();
					$("#code").parents(".form-group").hide();
					$("#icon").parents(".form-group").show();
					$("#target").parents(".form-group").hide();
					$("input[name='visible']").parents(".form-group").show();
					$(".is-refresh").hide();
				} else if (menuType == "2") {
					$("#parentMenuId").parents(".form-group").show();
					loadMenuLevel("parentMenuId",parentMenuId,1);

					$("#menuUrl").parents(".form-group").show();
					$("#code").parents(".form-group").show();
					$("#icon").parents(".form-group").show();
					$("#target").parents(".form-group").show();
					$("input[name='visible']").parents(".form-group").show();
					$(".is-refresh").show();
				} else if (menuType == "3") {

					$("#parentMenuId").parents(".form-group").show();
					loadMenuLevel("parentMenuId",parentMenuId,2);

					$("#menuUrl").parents(".form-group").hide();
					$("#code").parents(".form-group").show();
					$("#icon").parents(".form-group").hide();
					$("#target").parents(".form-group").hide();
					$("input[name='visible']").parents(".form-group").hide();
					$(".is-refresh").hide();
				}
			}

		</script>
	</body>
</html>
